<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Log;
use DB;
use App\Http\Models\UserIntegralLog;
use App\Http\Models\UserCapital;
use App\Http\Models\VoucherLog;
use App\Console\Commands\importqq;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        Commands\importqq::class,
        Commands\importuser::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //每月1号进行将用户积分归为20
        $schedule->call(function () {
            $bool = DB::table('users_capital')->update(['integral' => env('UserIntegral'), 'voucher_number' => env('Voucher_Default_Give')]);
            $statistic_bool = DB::table('statistics_user')->update(['zreo_integral_user' => 0]);
            $user_capital = DB::table('users_capital')->get();
            foreach ($user_capital as $key => $value) {
                UserIntegralLog::addUserIntegralLog($value->user_id, '每月1号初始化积分为20', env('UserIntegral'), 0, 1, $refuse = '');
                VoucherLog::addUserVoucherLog($value->user_id, '每月1号初始化点券为' . env('Voucher_Default_Give'), env('Voucher_Default_Give'), 2, 0, '');
            }
            if ($bool && $statistic_bool) {
                Log::info('每月1号更新用户积分!');
            }
        })->monthlyOn(1, '00:00')->description('每月1号更新用户积分以及点券');

        //每周周一凌点删除前一周的销量数据
        $schedule->call(function () {
            $last_week_time = date('Y-m-d H:i:s', strtotime(date('Y-m-d') . ' 00:00:00') - 3 * 24 * 3600);
            $bool = DB::table('statistics_sales_log')
                //->whereDate('updated_at', date('Y-m-d',strtotime('-1 week')))
                ->where('created_at', '<=', $last_week_time)
                ->delete();
            if ($bool) {
                Log::info('每周周一凌点删除前一周的销量数据!');
            }
        })->dailyAt('00:10')->description('每天清除前3天商品销量数据记录');


        //每天将统计的今日数据清0
        /* $schedule->call(function () {
             $is_s = StatisticsGoodsToday::where(['date' => date('Ymd')])->first();
             if (!$is_s) {
                 $goods_bool = DB::table('statistics_goods_today')->insert([
                     'date' => date('Ymd'),
                     'today_ordinary_unaudited' => 0,
                     'today_ordinary_adopt' => 0,
                     'today_ordinary_refuse' => 0,
                     'today_bursting_refuse' => 0,
                     'today_bursting_adopt' => 0,
                     'today_bursting_unaudited' => 0,
                     'today_kill_adopt' => 0,
                     'today_kill_refuse' => 0,
                     'today_kill_unaudited' => 0,
                     'created_at' => date('Y-m-d H:i:s')
                 ]);
                 if ($goods_bool) {
                     Log::info('更新商品数据统计!');
                 }
             }
             $error_bool = StatisticsError::ErrorModel();
             if ($error_bool) {
                 Log::info('更新错误统计!');
             }
             $bool = DB::table('statistics_user_voucher')->update(['day_voucher' => 0]);
             if ($bool) {
                 Log::info('更新用户日消费额!');
             }
         })->daily();*/
        //测试调度能否在执行执行
        /*$schedule->call(function () {
            $model = DB::table('test')->find(1);
            if ($model) {
                DB::table('test')->where(['id'=>1])->update(['col1'=>$model->col1+1]);
            }
        })->everyMinute();*/
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');

        require base_path('routes/console.php');
    }


}
